/**
 *
 */
package com.cmcc.awareness;

import java.util.ArrayList;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Date 2018年4月4日 下午4:58:44
 * @author Amish
 *
 */
public class HttpProtoRule {
	private static Logger logger = LogManager.getLogger(HttpProtoRule.class);
	/**
	 *  [QIYI_HTTP_Video-66]
		##Rule_ID:11514
		name=QIYI_HTTP_Video
		Host=meta.video.qiyi.com
		Uri=.m3u8
	 */

	public String appName;//对应proto.list中的业子项名称，如QIYI_Browsing、WeiXin_VoIP_Voice
	public String rule_id;
	public String priority = "0";
	public HttpProtoRuleField httpProtoRuleField;//当某个Rule中只有一个Field时,该变量不为null；
	public ArrayList<HttpProtoRuleField> ruleFields;//当某个Rule中有大于一个Field时,该变量不为null；
	
	
	public boolean distinguish(org.apache.http.HttpRequest httpRequest){
		if(httpProtoRuleField != null){
			boolean match = httpProtoRuleField.distinguish(httpRequest);
			if(match){
				logger.debug("HttpProtoRuleField match : "+httpProtoRuleField.getField()+"="+httpProtoRuleField.getRuleStr()+" ruleRegex: <"+httpProtoRuleField.getRuleStrRegex()+" >");
			}
			return match;
		}else{
			boolean match = true;
			for(HttpProtoRuleField httpProtoRuleField : ruleFields){
				match = match & httpProtoRuleField.distinguish(httpRequest);
			}
			if(match){
				for(HttpProtoRuleField httpProtoRuleField : ruleFields){
					logger.debug("HttpProtoRuleField match : "+httpProtoRuleField.getField()+"="+httpProtoRuleField.getRuleStr()+" ruleRegex: <"+httpProtoRuleField.getRuleStrRegex()+" >");
				}
			}
			return match;
		}
	}
	
	public boolean distinguish(String https_Tls_server , String https_id_at_commonName){
		if(httpProtoRuleField != null){
			boolean match = httpProtoRuleField.distinguish(https_Tls_server , https_id_at_commonName);
			if(match){
				logger.debug("HttpProtoRuleField match : "+httpProtoRuleField.getField()+"="+httpProtoRuleField.getRuleStr()+" ruleRegex: <"+httpProtoRuleField.getRuleStrRegex()+" >");
			}
			return match;
		}else{
			boolean match = true;
			for(HttpProtoRuleField httpProtoRuleField : ruleFields){
				match = match & httpProtoRuleField.distinguish(https_Tls_server , https_id_at_commonName);
			}
			if(match){
				for(HttpProtoRuleField httpProtoRuleField : ruleFields){
					logger.debug("HttpProtoRuleField match : "+httpProtoRuleField.getField()+"="+httpProtoRuleField.getRuleStr()+" ruleRegex: <"+httpProtoRuleField.getRuleStrRegex()+" >");
				}
			}
			return match;
		}
	}
	
	/**
	 * output eg
	 * [QIYI_Browsing-4]
       #Rule_ID:24773
       Host=iqiyi.irs01.com
       name=QIYI_Browsing
	 */
	public String toString(){
		//TODO
		return null;
	}

}
